#! usr/bin/env python3
# coding=utf-8
"""
Generate the visualized picture of resampled trajectory points, which
is going to be displayed at the web page.
"""

from PIL import Image, ImageDraw
import trajectory_preprocess as tra_pre

IMG_SIZE = (100, 100)


def draw_tra(points):
    """:param points Expecting array of resampled points
    Return -> PIL.Image"""
    R = 3  # the radius of joints
    im = Image.new(mode='RGB', size=IMG_SIZE)

    dw = ImageDraw.Draw(im, mode='RGB')
    dw.line(tuple(points.reshape([16])), fill=(255, 255, 0), width=1)
    dw.ellipse((tuple(points[0]-R), tuple(points[0]+R)), fill=(255, 255, 255))
    for i in range(1, len(points)):
        dw.ellipse((tuple(points[i]-R), tuple(points[i]+R)), fill=(255, 0, 0))

    return im


def get_points(filename):
    """Get the points from the file, and do some pre process"""
    p = tra_pre.PreProcessor()
    with open(filename, 'r', encoding="utf-8") as f:
        line = f.readline()
        p.load(line)
        f.close()
    points = p.export(if_reshape=False)
    points = p.coordination_translate(points)

    return points


if __name__ == '__main__':
    tra = get_points('sample.txt')
    print("the tra", tra)
    img = draw_tra(tra)
    img.save('test.png')




